import { reactive, watch } from 'vue';
import { mmdd } from 'utils/date';
import { uuid } from 'utils/utils';
import { ElMessage } from 'element-plus';
export default function (emit) {
  const state = reactive({
    project: {
      name: '',
      startAt: new Date(),
      listName: '',
      daily: 2,
      total: 42,
      gap: ['1', '2', '4', '8', '15', '26'],
      reverse: false,
    },
    alternatives: {
      chosen: '自定义',
      presets: ['自定义', '红宝书考研 必考词', '红宝书考研 基础词', '新东方考研', '朱伟5500词'],
    },
  });

  const { project, alternatives } = state;

  watch(
    () => alternatives.chosen,
    (newValue) => {
      if (newValue === alternatives.presets[1]) {
        project.name = newValue;
        project.total = 26;
        project.listName = '必考词';
      } else if (newValue === alternatives.presets[2]) {
        project.name = newValue;
        project.total = 31;
        project.listName = '基础词';
      } else {
        project.name = '';
        project.listName = '';
        project.total = 1;
      }
    },
  );

  let handleAddProject = () => {
    if (/^\s*$/.test(project.name)) project.name = '默认计划';
    if (/^\s*$/.test(project.listName)) project.listName = 'Unit';
    project.startAtmmdd = mmdd(project.startAt);
    project.uuid = uuid();

    emit('add-project', project);

    ElMessage({
      message: `${project.name} 已成功添加！`,
      type: 'success',
    });
    project.name = '';
    project.listName = '';
  };

  let handleRestoreDefault = () => {
    project.gap = ['1', '2', '4', '8', '15', '26'];
  };
  return {
    project,
    alternatives,
    handleAddProject,
    handleRestoreDefault,
  };
}
